
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>synchronized 和 Lock 的区别 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>synchronized 和 Lock 的区别 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">Java中的`synchronized`和`Lock`在锁的管理方式上有什么根本区别？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`synchronized`是隐式锁，由JVM自动管理锁的获取和释放。而`Lock`是显式锁，需要开发者通过`lock()`和`unlock()`方法手动获取和释放锁。</div>
          </div>
          <div class="card-source">来源: 文档中「1. synchronized 关键字」和「2. Lock 接口」部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">`synchronized`关键字有哪些主要优点？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">它的主要优点是简单易用，代码编写更直观，并且锁的释放由JVM自动管理，减少了因忘记释放锁而导致的编程错误。</div>
          </div>
          <div class="card-source">来源: 文档中「1. synchronized 关键字」的“优点”部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">相比`synchronized`，`Lock`接口提供了哪些更高级的锁功能？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Lock`接口提供了`tryLock()`（尝试锁定并可设置超时）、`lockInterruptibly()`（可中断锁）以及通过`ReentrantLock`实现公平锁等高级功能，这些是`synchronized`不具备的。</div>
          </div>
          <div class="card-source">来源: 文档中「2. Lock 接口」的“特性”部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">为什么在使用`Lock`时，强烈建议将`unlock()`方法调用放在`finally`代码块中？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">为了确保锁在任何情况下都能被释放，即使在临界区代码（try块）中发生异常，`finally`块中的`unlock()`也总会被执行，从而有效避免死锁。</div>
          </div>
          <div class="card-source">来源: 文档中「3. 对比示例」的`ReentrantLock`示例代码</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在什么场景下，使用`synchronized`可能是更合适的选择？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当同步需求较简单，对锁控制要求不高时，`synchronized`是合适的选择，因为其语法简单且能自动管理锁，不易出错。</div>
          </div>
          <div class="card-source">来源: 文档中「3. 对比示例」的总结部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">什么情况下应该优先考虑使用`Lock`接口而不是`synchronized`？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当需要更复杂的锁机制和更高的性能时，应考虑使用`Lock`。例如，需要尝试锁定、可中断锁、超时等待或实现公平锁的场景。</div>
          </div>
          <div class="card-source">来源: 文档中「3. 对比示例」的总结部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">`synchronized`的一个主要缺点是什么，尤其是在高并发场景下？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">它的主要缺点是灵活性较低，并且在高并发场景下，性能可能会受到影响，因为它在进入和退出同步块时都会进行锁操作。</div>
          </div>
          <div class="card-source">来源: 文档中「1. synchronized 关键字」的“缺点”部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">使用`Lock`接口相比`synchronized`会带来什么潜在的风险？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">主要风险是增加了编程的复杂性。由于需要显式调用`lock()`和`unlock()`，如果开发者忘记释放锁（尤其是在异常情况下），很容易引发死锁问题。</div>
          </div>
          <div class="card-source">来源: 文档中「2. Lock 接口」的“缺点”部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">`ReentrantLock`是如何实现“公平锁”的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">文档提到`ReentrantLock`提供了公平锁的选项，其机制是确保线程按照请求锁的顺序来获得锁，先请求的线程会先获得锁。</div>
          </div>
          <div class="card-source">来源: 文档中「2. Lock 接口」的“特性”部分</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
